﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DentScan2.DB;

namespace DentScan2.WebApp.Classes
{
    public class Car
    {
        public Guid? CarId
        {
            get;
            set;
        }


        public Owner Owner
        {
            get;
            set;
        }

        public Guid? ModelId
        {
            get;
            set;
        }

        public string ModelName
        {
            get;
            set;
        }

        public Guid? MakeId
        {
            get;
            set;
        }

        public string MakeName
        {
            get;
            set;
        }

        public string RegNumber
        {
            get;
            set;
        }
        public string ChassisNumber
        {
            get;
            set;
        }

        public Guid? CarTypeId
        {
            get;
            set;
        }

        public string CarTypeName
        {
            get;
            set;
        }


        public int? ProductionYear
        {
            get;
            set;
        }

        public Guid? ColorId
        {
            get;
            set;
        }

        public string ColorName
        {
            get;
            set;
        }

        public Guid? PaintId
        {
            get;
            set;
        }

        public string PaintName
        {
            get;
            set;
        }


        public string Doors
        {
            get;
            set;
        }
        public Car()
        {
            this.Owner = new Owner();
        }

        public Car(DB.Car car)
        {
            this.Owner = new Owner(car.Owner);
            this.CarId=car.CarId;
            this.CarTypeId=car.CarType.CarTypeId;
            this.CarTypeName=car.CarType.CarTypeName;
            this.ChassisNumber=car.ChassisNumber;
            this.ColorId=car.Color.ColorId;
            this.ColorName=car.Color.ColorName;
            this.Doors=car.Doors;
            this.MakeId=car.Model.Make.MakeId;
            this.MakeName=car.Model.Make.MakeName;
            this.ModelId=car.Model.ModelId;
            this.ModelName=car.Model.ModelName;
            this.PaintId=car.Paint.PaintId;
            this.PaintName=car.Paint.PaintName;
            this.ProductionYear=car.ProductionYear;
            this.RegNumber=car.RegNumber;            

        }

        public void SaveToDB(DLDataContext dl)
        {
            this.Owner.SaveToDB(dl);
            if (this.CarId == null)
                Insert(dl);
            else
                Update(dl);
        }

        private void Update(DLDataContext dl)
        {
            var item = dl.Cars.Where(c => c.CarId == this.CarId).First();
            item.CarTypeId = this.CarTypeId.Value;            
            item.ChassisNumber = this.ChassisNumber;
            item.ColorId = this.ColorId;
            item.Doors = this.Doors;
            item.ModelId = this.ModelId.Value;
            item.PaintId = this.PaintId;
            item.ProductionYear = this.ProductionYear;
            item.RegNumber = this.RegNumber;        
            item.IsDel = false;
            item.LastEditDate = DateTime.UtcNow;
            dl.SubmitChanges();
        }

        private void Insert(DLDataContext dl)
        {
            var item = new DB.Car();
            item.CarId = Guid.NewGuid();
            item.OwnerId = this.Owner.OwnerId.Value;
            item.CarTypeId = this.CarTypeId.Value;
            item.ChassisNumber = this.ChassisNumber;
            item.ColorId = this.ColorId;
            item.Doors = this.Doors;
            item.ModelId = this.ModelId.Value;
            item.PaintId = this.PaintId;
            item.ProductionYear = this.ProductionYear;
            item.RegNumber = this.RegNumber;        

            item.IsDel = false;
            item.LastEditDate = DateTime.UtcNow;
            dl.Cars.InsertOnSubmit(item);
            dl.SubmitChanges();
            this.CarId = item.CarId;
        }

    }
}